﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using SelfMadeMillionaire.Core.Enums;

namespace SelfMadeMillionaire.Core.Entities
{
    public class Subscriber : BaseEntity
    {
        [StringLength(100)]
        public string FirstName { get; set; }
        [StringLength(100)]
        public string LastName { get; set; }
        [StringLength(100)]
        public string Email { get; set; }
        [StringLength(100)]
        public string PasswordHash { get; set; }
        public byte ProductType { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime? UnsubscribeDate { get; set; }
        public byte? SubscriptionLevel { get; set; }
        public DateTime? StartDate { get; set; }
        public DateTime? EndDate { get; set; }
        public Guid? Token { get; set; }
        public Guid? PasswordRecoveryKey { get; set; }

        public virtual ICollection<EmailMessage> EmailMessages { get; set; }
        public virtual ICollection<UserSession> UserSessions { get; set; }

        public bool SmmUnsubscribed { get; set; }
        public bool MmmUnsubscribed { get; set; }
        public bool TesterAccount { get; set; }

        [StringLength(100)]
        public string TokenApi { get; set; }
        [StringLength(256)]
        public string DeviceKey { get; set; }
        [StringLength(50)]
        public string DeviceType { get; set; }
        public DateTime? LastPingDateTime { get; set; }

        [StringLength(50)]
        public string Mailing { get; set; }

        public bool IsFreeMMMSubscriber()
        {
            return !MmmUnsubscribed;
        }

        public bool IsSMMSubscriber()
        {
            return //((ProductType & (byte)SelfMadeMillionaire.Core.Enums.ProductType.SelfMadeMillionaire) == ProductType)
                !SmmUnsubscribed
                && SubscriptionLevel.HasValue;
        }

        public bool IsFreeSMMSubscriber()
        {
            return //((ProductType & (byte)SelfMadeMillionaire.Core.Enums.ProductType.SelfMadeMillionaire) == ProductType)
                !SmmUnsubscribed
                && SubscriptionLevel.HasValue
                && SubscriptionLevel.Value == (byte)SubscriptionType.Free;
        }

        public bool IsSilverSubscriber()
        {
            return ((ProductType & (byte)SelfMadeMillionaire.Core.Enums.ProductType.SelfMadeMillionaire) == (byte)SelfMadeMillionaire.Core.Enums.ProductType.SelfMadeMillionaire)
                && !SmmUnsubscribed
                && SubscriptionLevel.HasValue
                && (SubscriptionLevel.Value == (byte)SubscriptionType.Silver);
        }

        public bool IsGoldSubscriber()
        {
            return ((ProductType & (byte)SelfMadeMillionaire.Core.Enums.ProductType.SelfMadeMillionaire) == (byte)SelfMadeMillionaire.Core.Enums.ProductType.SelfMadeMillionaire)
                && !SmmUnsubscribed
                && SubscriptionLevel.HasValue
                && (SubscriptionLevel.Value == (byte)SubscriptionType.Gold);
        }

        public bool IsPlatinumSubscriber()
        {
            return ((ProductType & (byte)SelfMadeMillionaire.Core.Enums.ProductType.SelfMadeMillionaire) == (byte)SelfMadeMillionaire.Core.Enums.ProductType.SelfMadeMillionaire)
                && !SmmUnsubscribed
                && SubscriptionLevel.HasValue
                && (SubscriptionLevel.Value == (byte)SubscriptionType.Platinum);
        }

        public string SubscriptionLevelName
        {
            get
            {
                string result = "";
                bool isSmm = (!SmmUnsubscribed);
                if (isSmm)
                {
                    result = ((SubscriptionType?)SubscriptionLevel).ToString();
                }

                return result;
            }
        }
    }
}
